<template>
  <el-dialog :title="title" :visible.sync="visible" width="800px" append-to-body custom-class="import-dialog"
  >
    <el-form ref="form" :model="form" label-width="120px" :rules="rules">
      <el-form-item label="开户人" prop="accountHolder">
        <el-input v-model="form.accountHolder" maxlength="20" show-word-limit></el-input>
      </el-form-item>
      <el-form-item label="银行账户" prop="bankAccount">
        <el-input v-model="form.bankAccount" maxlength="19"></el-input>
      </el-form-item>
      <el-form-item label="开户银行" prop="bankName">
        <el-select v-model="bankNumber" style="width: 100%;" :filter-method="filterBank" filterable
                   @change="selectBankChange" @focus="filterBank()"
        >
          <el-option :label="item.bankName" :value="item.bankNumber" v-for="item in bankList" :key="item.id"
          ></el-option>

          <pagination style="text-align: center" v-show="bankTotal > 0" :total="bankTotal"
                      :page.sync="bankQueryParams.pageNum"
                      :limit.sync="bankQueryParams.pageSize"
                      layout="total, prev, pager, next, jumper"
                      :pager-count="5"
                      @pagination="getBankList"
          />

        </el-select>
      </el-form-item>
      <el-form-item label="银行支行" prop="bankBranch">
        <el-select v-model="branchNumber" style="width: 100%;" filterable :filter-method="filterBranch"
                   @change="selectBranchChange" @focus="filterBranch()"
        >
          <el-option :label="item.branchName" :value="item.branchNumber" v-for="item in bankBranchList" :key="item.id">
          </el-option>
          <pagination style="text-align: center" v-show="bankBranchTotal > 0" :total="bankBranchTotal"
                      :page.sync="branchQueryParams.pageNum"
                      :limit.sync="branchQueryParams.pageSize"
                      layout="total, prev, pager, next, jumper"
                      :pager-count="5"
                      @pagination="getBankBranchList"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取 消</el-button>
      <el-button type="primary" @click="onSubmit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { debounce } from '@/utils'
import { addCommission } from '@/api/contract/proprietor'
import { listBank, listBankBranch } from '@/api/account/yima'

export default {
  name: 'house-entrust',
  props: {
    title: {
      default: '房屋委托信息'
    },
    value: {
      default: false
    },
    data: {
      default: () => ({})
    }
  },
  data() {
    return {
      form: {},
      //银行筛选条件
      bankQueryParams: {
        pageNum: 1,
        pageSize: 20,
        bankName: ''
      },
      //银行
      bankNumber: '',
      bankList: [],
      bankTotal: 0,

      //支行筛选条件
      branchQueryParams: {
        pageNum: 1,
        pageSize: 20,
        branchName: '',
        bankNumber: ''
      },
      //支行
      branchNumber: '',
      bankBranchList: [],
      bankBranchTotal: 0,
      rules: {
        accountHolder: [
          { required: true, message: '请输入开户人', trigger: 'blur' },
        ],
        bankAccount: [
          { required: true, message: '请输入银行账户', trigger: 'blur' },
        ],
        bankName: [
          { required: true, message: '请输入开户银行', trigger: 'blur' },
          { min: 2, max: 50, message: '长度在 2 到 50 个字符', trigger: 'blur' }
        ],
        bankBranch: [
          { required: true, message: '请输入银行支行', trigger: 'blur' },
        ]
      }
    }
  },
  computed: {
    visible: {
      get() {
        return this.value
      },
      set(val) {
        this.$emit('input', val)
      }
    }
  },
  watch: {
    value: {
      handler(newVal, oldVal) {
        if (newVal) {
          this.init()
        }
      },
      immediate: true
    }
  },
  methods: {
    init() {
      this.reset()
      this.getBankList()
    },
    reset() {
      this.form = {
        proprietorId: this.data.proprietorId,
        accountHolder: '',
        bankAccount: '',
        bankName: '',
        bankBranch: ''
      }
      this.bankNumber = ''
      this.branchNumber = ''
    },
    //获取银行列表
    getBankList() {
      listBank(this.bankQueryParams).then(res => {
        this.bankList = res.rows
        this.bankTotal = res.total
      })
    },
    //获取支行列表
    getBankBranchList() {
      listBankBranch(this.branchQueryParams).then(res => {
        this.bankBranchList = res.rows
        this.bankBranchTotal = res.total
      })
    },
    //选择银行
    filterBank: debounce(function(item) {
      console.log('item', item)
      this.bankQueryParams.bankName = item
      this.bankQueryParams.pageNum = 1
      this.getBankList()
    }, 500, true),
    //选择银行后，获取支行列表
    selectBankChange(val) {
      let bank = this.bankList.find(item => item.bankNumber === val)
      this.form.bankName = bank.bankName
      this.branchQueryParams.bankNumber = bank.bankNumber //设置支行对应的银行

      //清空支行筛选条件
      this.branchNumber = ''
      this.branchQueryParams.branchName = ''
      this.branchQueryParams.pageNum = 1
      this.bankBranchList = []
      this.bankBranchTotal = 0
    },

    //筛选支行数据回调
    filterBranch: debounce(function(query) {
      this.branchQueryParams.branchName = query
      this.getBankBranchList()
    }, 500, true),
    //选择支行后
    selectBranchChange(val) {
      let branch = this.bankBranchList.find(item => item.branchNumber == val)
      this.form.bankBranch = branch.branchName
    },

    onSubmit: debounce(function() {
      this.$refs.form.validate(valid => {
        if (!valid) return
        addCommission(this.form).then(res => {
          this.$message.success(res.msg)
          this.visible = false
          this.$emit('success', res)
        })
      })
    }, 500, true)
  }
}
</script>

<style scoped lang="scss">
.mobile-tips {
  font-size: 12px;
  color: #999;
  margin-left: 10px;
}

.pagination-container {
  text-align: center;
}
</style>
